/*
	Copyright (c) 2010 <copyright holders>
	This source code is licensed under The MIT License,
	see the file LICENSE for details.
	*/

#include "MenuPresentation.hpp"

#include <OGRE/OgreRoot.h>
#include <QuickGUIKeyCode.h>
#include <QuickGUIManager.h>

#include "CameraMan.hpp"
#include "FixedTerrainHeader.hpp"
#include "Game.hpp"
#include "gen/netmsg.pb.h"
#include "MenuGUI.hpp"
#include "MenuNetLogic.hpp"
#include "WorldManager.hpp" //TODO: see if we can get rid of this dependency

namespace spelprog2 {
namespace presentation {

MenuPresentation::MenuPresentation(Game & game, const std::string & mapName) : Presentation(game),
    mapName_(mapName),
    sceneMan_(dynamic_cast<Ogre::TerrainSceneManager *>(game.getRoot().createSceneManager("TerrainSceneManager"))),
    gui_()
{
}

MenuPresentation::~MenuPresentation()
{
    gui_.reset();
    getGame().getRoot().destroySceneManager(sceneMan_);
}

void MenuPresentation::initScene()
{
    getGame().getWorldManager().startBackground(sim::Int(0), mapName_);
}

void MenuPresentation::initCameraManager()
{
    cameraMan_.reset(new CameraMan(getGame().getCamera(), getGame().getWorldManager().getLandscape()));
    //getGame().getCamera().setPosition(Ogre::Vector3(0.0f, 120.0f, 0.0f));
    //getGame().getCamera().lookAt(Ogre::Vector3(1000.0f, -1000.0f, 1000.0f));
}

void MenuPresentation::initGUI()
{
    gui_.reset(new gui::MenuGUI(getGame(), *sceneMan_));
}

void MenuPresentation::registerNetListeners()
{
    gui_->registerNetListeners();
}

void MenuPresentation::unregisterNetListeners()
{
    gui_->unregisterNetListeners();
}

void MenuPresentation::keyPressed(const OIS::KeyEvent & ev) {
    gui_->getManager().injectKeyDown(QuickGUI::KeyCode(ev.key));
    gui_->getManager().injectChar(ev.text);
}

void MenuPresentation::keyReleased(const OIS::KeyEvent & ev) {
    gui_->getManager().injectKeyUp(QuickGUI::KeyCode(ev.key));
}

void MenuPresentation::mouseMoved(const Ogre::Vector2 & point, const Ogre::Vector2 & normalizedPoint,
    const OIS::MouseEvent & ev)
{
    gui_->getManager().injectMousePosition(int(point.x), int(point.y));
}

void MenuPresentation::mousePressed(const Ogre::Vector2 & point, const Ogre::Vector2 & normalizedPoint,
    const OIS::MouseEvent & ev, OIS::MouseButtonID id)
{
    gui_->getManager().injectMouseButtonDown(QuickGUI::MouseButtonID(id));
}

void MenuPresentation::mouseReleased(const Ogre::Vector2 & point, const Ogre::Vector2 & normalizedPoint,
    const OIS::MouseEvent & ev, OIS::MouseButtonID id)
{
    gui_->getManager().injectMouseButtonUp(QuickGUI::MouseButtonID(id));
}

}; //presentation
}; //spelprog2
